package com.spzx.product;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.pojo.ProductSku;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author XC
 * @version 1.0
 * @description 商品模块
 * @knowledge
 */
@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
@Slf4j
public class SpzxProductApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class, args);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    private ProductSkuMapper productSkuMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("程序启动");
        // 从 productSKu中查询所有已发布的商品 id，并放入 bitmap中
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<ProductSku>()
                .eq(ProductSku::getStatus, 1);

        List<ProductSku> productSkuList = productSkuMapper.selectList(queryWrapper);

        // 获取 productSkuList中每个 productSku的 id，并设置偏移量上的位
        productSkuList.forEach(productSku -> {
            redisTemplate.opsForValue().setBit("product:sku:data:ids", productSku.getId(), true);
        });
    }
}
